package com.howells.time.base.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;

import com.howells.time.base.common.AppContext;


/**
 * Created by Administrator on 2016.3.20.
 */
public class Utils {
    private static final String TAG = "Utils";

    // 获取ApiKey
    public static String getMetaValue(Context context,String metakey){
        Bundle metaData=null;
        String apikey=null;
        if(context==null||metakey==null){
            return null;
        }
        try{
            ApplicationInfo ai=context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            if(null!=ai){
                metaData=ai.metaData;
            }
            if(null!=metaData){
                apikey=metaData.getString(metakey);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return apikey;
    }

    /**
     * 设置手机网络类型，wifi，cmwap，ctwap，用于联网参数选择
     * @return
     */
    public static String getNetworkType(){
        String networkType="wifi";
        ConnectivityManager manager= (ConnectivityManager) AppContext.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo=manager.getActiveNetworkInfo();
        if(networkInfo==null||!networkInfo.isAvailable()){
            return "";
        }

        String info=networkInfo.getExtraInfo();
        if((info!=null)
                &&(info.trim().toLowerCase().equals("cmwap"))
                ||(info.trim().toLowerCase().equals("uniwap"))
                ||(info.trim().toLowerCase().equals("3gwap"))
                ||(info.trim().toLowerCase().equals(("ctwap")))
                ){
            if(info.trim().toLowerCase().equals("ctwap")){
                networkType="ctwap";
            }else {
                networkType="cmwap";
            }
        }
        return networkType;
    }

    /**
     * 检测网络是否可用
     *
     * @param context
     * @return
     */
    public static boolean isNetWorkConnected(Context context){
        if(context!=null){
            ConnectivityManager connectivityManager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
            if(networkInfo!=null){
                return networkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 检测Sdcard是否存在
     *
     * @return
     */
    public static boolean isExitsSdcard(){
        if(android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
            return true;
        else
            return false;
    }

    public static String getString(Context context,int resId){
        return context.getResources().getString(resId);
    }
}
